﻿using System;
using System.Collections.Generic;

namespace Core.Library
{
    public class DateTimeFunctions
    {
        /// <summary>
        /// Formats the date vn (dd*mm*yy).
        /// </summary>
        /// <param name="dt">Datetime.</param>
        /// <param name="strType">Separator character.</param>
        /// <returns></returns>
        public static string FormatDateVietNam(DateTime dt, string strType)
        {
            return string.Format("{0}{3}{1}{3}{2}", dt.Day, dt.Month, dt.Year, strType);
        }

        /// <summary>
        /// Formats the date viet nam (dd-mm-yy).
        /// </summary>
        /// <param name="dt">Datetime.</param>
        /// <returns></returns>
        public static string FormatDateVietNam(DateTime dt)
        {
            return string.Format("{0}-{1}-{2}", dt.Day, dt.Month, dt.Year);
        }

        /// <summary>
        /// Formats the time viet nam (hh:mm:ss).
        /// </summary>
        /// <param name="dt">Datetime.</param>
        /// <returns></returns>
        public static string FormatTimeVietNam(DateTime dt)
        {
            return string.Format("{0}:{1}:{2}", dt.Hour, dt.Minute, dt.Second);
        }

        /// <summary>
        /// Valids the date time, string date => DateTime.
        /// </summary>
        /// <param name="dt">String date.</param>
        /// <returns></returns>
        public static DateTime ValidDateTime(string dt)
        {
            var isCheck = dt.Contains("/");
            var splitDateTime = dt.Split(isCheck ? '/' : '-');

            var validDateTime = string.Format("{0}/{1}/{2}", splitDateTime[2], splitDateTime[1], splitDateTime[0]);
            return ConvertUtility.ToDateTime(validDateTime);
        }

        /// <summary>
        /// Valids the date time, string date, string hhmmss => DateTime.
        /// </summary>
        /// <param name="date">string date.</param>
        /// <param name="hhmmsss">string hhmmss.</param>
        /// <returns></returns>
        public static DateTime ValidDateTime(string date, string hhmmsss)
        {
            var isCheck = date.Contains("/");
            var splitDateTime = date.Split(isCheck ? '/' : '-');

            var validDateTime = string.Format("{0}/{1}/{2} {3}", splitDateTime[2], splitDateTime[1], splitDateTime[0], hhmmsss);
            return ConvertUtility.ToDateTime(validDateTime);
        }

        /// <summary>
        /// Formats datetime (hh:mm - dd/mm/yyyy).
        /// </summary>
        /// <param name="dt">DateTime.</param>
        /// <returns></returns>
        public static string FormatIn_HHMM_DDMMYYYY(DateTime dt)
        {
            string sDay;
            string sMonth;
            string sHour;
            string sMin;

            if (dt.Hour < 10)
            {
                sHour = "0" + dt.Hour;
            }
            else
                sHour = dt.Hour.ToString();
            if (dt.Minute < 10)
            {
                sMin = "0" + dt.Minute;
            }
            else
                sMin = dt.Minute.ToString();
            if (dt.Month < 10)
            {
                sMonth = "0" + dt.Month;
            }
            else
                sMonth = dt.Month + "";
            if (dt.Day < 10)
            {
                sDay = "0" + dt.Day;
            }
            else
                sDay = dt.Day + "";

            return sHour + ":" + sMin + " - " + sDay + "/" + sMonth + "/" + dt.Year;
        }

        /// <summary>
        /// Formats datetime (hh:mm).
        /// </summary>
        /// <param name="dt">DateTime.</param>
        /// <returns></returns>
        public static string FormatIn_HHMM(DateTime dt)
        {
            string sHour;
            string sMin;

            if (dt.Hour < 10)
            {
                sHour = "0" + dt.Hour;
            }
            else
                sHour = dt.Hour.ToString();
            if (dt.Minute < 10)
            {
                sMin = "0" + dt.Minute;
            }
            else
                sMin = dt.Minute.ToString();

            return sHour + ":" + sMin;
        }

        /// <summary>
        /// Formats datetime (hh:mm:ss).
        /// </summary>
        /// <param name="dt">DateTime.</param>
        /// <returns></returns>
        public static string FormatIn_HHMMSS(DateTime dt)
        {
            string sHour;
            string sMin;
            string sSecond;

            if (dt.Hour < 10)
            {
                sHour = "0" + dt.Hour;
            }
            else
                sHour = dt.Hour.ToString();
            if (dt.Minute < 10)
            {
                sMin = "0" + dt.Minute;
            }
            else
                sMin = dt.Minute.ToString();
            if (dt.Second < 10)
            {
                sSecond = "0" + dt.Second;
            }
            else
                sSecond = dt.Second.ToString();

            return sHour + ":" + sMin + ":" + sSecond;
        }

        /// <summary>
        /// Formats datetime (dd/mm/yyyy).
        /// </summary>
        /// <param name="dt">DateTime.</param>
        /// <returns></returns>
        public static string FormatIn_DDMMYYYY(DateTime dt)
        {
            string sDay;
            string sMonth;
            string sHour;
            string sMin;

            if (dt.Hour < 10)
            {
                sHour = "0" + dt.Hour;
            }
            else
                sHour = dt.Hour.ToString();
            if (dt.Minute < 10)
            {
                sMin = "0" + dt.Minute;
            }
            else
                sMin = dt.Minute.ToString();
            if (dt.Month < 10)
            {
                sMonth = "0" + dt.Month;
            }
            else
                sMonth = dt.Month + "";
            if (dt.Day < 10)
            {
                sDay = "0" + dt.Day;
            }
            else
                sDay = dt.Day + "";

            return sDay + "/" + sMonth + "/" + dt.Year;
        }

        /// <summary>
        /// The date time to stamp
        /// </summary>
        /// <param name="dateTime">The date time.</param>
        /// <returns></returns>
        public static double DateTimeToStamp(DateTime dateTime)
        {
            //create Timespan by subtracting the value provided from
            //the Unix Epoch
            TimeSpan span = (dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

            //return the total seconds (which is a UNIX timestamp)
            return span.TotalSeconds;
        }

        /// <summary>
        /// Gets the short current datetime encode to 64UTF8 string.
        /// </summary>
        /// <returns></returns>
        public static string GetShortCurrentDateTime_EncodeTo64UTF8()
        {
            return
                UnicodeUtility.EncodeTo64UTF8(
                    DateTimeFunctions.ValidDateTime(
                        string.Format("{0}/{1}/{2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year),
                        "00:00:00").ToString());
        }

        public static Dictionary<string,string> GetListYear()
        {
            var r = new Dictionary<string, string>();
            for (int i = 1988; i < 2013; i++)
                r.Add(i.ToString(), i.ToString());
            return r;
        }

        public static Dictionary<string, string> GetListMonth()
        {
            var r = new Dictionary<string, string>();
            for (int i = 1; i < 13; i++)
                r.Add(i.ToString(), i.ToString());
            return r;
        }

        public static Dictionary<string, string> GetListDay()
        {
            var r = new Dictionary<string, string>();
            for (int i = 1; i < 32; i++)
                r.Add(i.ToString(), i.ToString());
            return r;
        }

        public static int CountAge(object dt)
        {
            var r = DateTime.Now;
            DateTime.TryParse(dt.ToString(), out r);
            return DateTime.Now.Year - r.Year;
        }
    }
}